package gui.utils;

import core.utils.EPara;
import gui.cen.Center;
import gui.processor.Processor;
import gui.processor.Worker;
import gui.win.tree.FullT;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;

/*Author: John Archer, Contact: john.archer.jpa@gmail.com*/
public class Slider extends JPanel implements ChangeListener{

    private JSlider slider;
    private int which;

    public static final int HPS_HIGHLIGHT  = 3;
    public static int MIN_FREQ_HIGHLIGHT = 4;
    public static int FRAME = 8;
    public static int KMER_LEN = 9;
    public static int KMER_DENSITY = 10;
    public static int WORD_SKIP = 11;
    public static int MAT = 12;
    public static int GO = 13;
    public static int GE = 14;
    public static int MIS = 16;
    public static int TMAG = 25;
    public static int ROTATE = 26;
    public static int TREEWIN = 29;
    public static int INDEL_LEN_VIEW = 30;
    public static int THREADS = 31;
    public static int VIEW_DEPTH = 35;
    public static int BUFF = 36;
    public static int TOLERATE = 37;
    public static int SPEED = 38;
    public static int POSTFILT = 39;
    public static int SEQSIZE = 40;
    public static int PREDHPSPLUSM = 41;
    public static int PREDHPSMINM = 42;
    public static int PREDHPSLEN = 43;

    private SliderWrap wrap;
    public Slider(int which, int maxVal, int minVal, int initialVal, SliderWrap os, int width){
        super();
        this.setOpaque(false);
        this.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
        wrap = os;
        this.setLayout(new BorderLayout());
        this.setBorder(null);
        this.which = which;
        slider = new JSlider(SwingConstants.HORIZONTAL, minVal, maxVal, initialVal);
        slider.setOpaque(false);
        slider.setPaintTicks(false);
        slider.setPaintLabels(false);
        slider.setPaintTrack(true);
        slider.addChangeListener(this);
        slider.setPreferredSize(new Dimension(width, 21));
        this.add(slider, BorderLayout.EAST);
        setVisible(true);
    }
    
 
    public Slider(int which, int maxVal, int minVal, int initialVal, int width, SliderWrap os){
        super();
        this.setOpaque(false);
        this.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
        wrap = os;
        this.setLayout(new BorderLayout());
        this.setBorder(null);
        this.which = which;

        slider = new JSlider(SwingConstants.HORIZONTAL, minVal, maxVal, initialVal);
        slider.setOpaque(false);
        slider.setPaintTicks(false);
        slider.setPaintLabels(false);
        slider.setPaintTrack(true);

        slider.addChangeListener(this);
        slider.setPreferredSize(new Dimension(width, 21));
        this.add(slider, BorderLayout.EAST);

        setVisible(true);
    }

    private FullT FullTreeL0;
    public Slider(int which, int maxVal, int minVal, int initialVal, int width, SliderWrap os, FullT FullTreeL0){
        super();
        this.setOpaque(false);
        this.FullTreeL0 = FullTreeL0;
        this.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
        wrap = os;
        this.setLayout(new BorderLayout());
        this.setBorder(null);
        this.which = which;
        slider = new JSlider(SwingConstants.HORIZONTAL, minVal, maxVal, initialVal);
        slider.setOpaque(false);
        slider.setPaintTicks(false);
        slider.setPaintLabels(false);
        slider.setPaintTrack(true);
        slider.addChangeListener(this);
        slider.setPreferredSize(new Dimension(width, 21));
        this.add(slider, BorderLayout.EAST);
        setVisible(true);
    }

    private Center center;
    public Slider(int which, int maxVal, int minVal, int initialVal, int width, SliderWrap os, Center center){
        super();
        this.setOpaque(false);
        this.center = center;
        this.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
        wrap = os;
        this.setLayout(new BorderLayout());
        this.setBorder(null);
        this.which = which;
        slider = new JSlider(SwingConstants.HORIZONTAL, minVal, maxVal, initialVal);
        slider.setOpaque(false);
        slider.setPaintTicks(false);
        slider.setPaintLabels(false);
        slider.setPaintTrack(true);
        slider.addChangeListener(this);
        slider.setPreferredSize(new Dimension(width, 21));
        this.add(slider, BorderLayout.EAST);
        setVisible(true);
    }

    public void stateChanged(ChangeEvent event){
        if(which==HPS_HIGHLIGHT){wrap.setLabel(EPara.HPS_LENGTH = slider.getValue());center.updateTable();}
        else if(which==THREADS){wrap.setLabel(EPara.THREADS = slider.getValue());}
        else if(which==KMER_LEN){wrap.setLabel(EPara.KMER_LEN = slider.getValue());}
        else if(which==KMER_DENSITY){wrap.setLabel(EPara.KMER_DENS = slider.getValue());}
        else if(which==WORD_SKIP){wrap.setLabel(EPara.WORD_SKIP = slider.getValue());}
        else if(which==MAT){wrap.setLabel(EPara.MAT = slider.getValue());}
        else if(which==GO){wrap.setLabel(EPara.GO = slider.getValue());}
        else if(which==GE){wrap.setLabel(EPara.GE  = slider.getValue());}
        else if(which==MIS){ wrap.setLabel(EPara.MIS = slider.getValue());}
        else if(which==MIN_FREQ_HIGHLIGHT){wrap.setLabelSpecial(EPara.MIN_FREQ_HIGHLIGHT = slider.getValue());center.updateTable();}
        else if(which==FRAME){
            wrap.setLabel(Para.FRAME = slider.getValue());
            Processor guip = new Processor(center);
            guip.setTask(Para.TASK_COUNT_STOP_CODONS);
            Worker sl = new Worker(guip);
            sl.execute();
            center.updateTable();
        }
        else if(which==TREEWIN){wrap.setLabel(Para.TREEWIN = slider.getValue());FullTreeL0.updateTextAndResetCodons();}
        else if(which == TOLERATE){wrap.setLabel(Para.TOLERATE = slider.getValue());}
        else if(which == BUFF){wrap.setLabel(Para.BUFF = slider.getValue());}
        else if(which==INDEL_LEN_VIEW){Para.INDEL_LEN_VIEW = slider.getValue();center.updatePlots();}
        else if(which==SPEED){Para.SPEED = slider.getValue();}
        else if(which==SEQSIZE){
            Borders.SEQFONTSIZE = slider.getValue();
            Borders.setCharHeightAndWidth();
            center.updateTable();
            center.updatePlots();
            try{center.updateReads();}catch(Exception e){}
            center.refreshContainer();
        }
        else if(which==POSTFILT){wrap.setLabelSpecial(EPara.POSTFILT = slider.getValue());center.updateTable();}
        else if(which==TMAG && Para.TREE!=null){
            wrap.setLabel(slider.getValue());
            Para.TREE.mag = slider.getValue();
            center.getTreeDisplay().getTreePane().repaint();}
        else if(which==ROTATE && Para.TREE!=null){
            wrap.setLabel(slider.getValue());
            Para.TREE.setCircularStartAngle((double)(slider.getValue()) * (Math.PI / 180));
            center.getTreeDisplay().getTreePane().repaint();
        }
        else if(which== PREDHPSPLUSM){wrap.setLabelSpecial(Para.PREDHPSPLUSM = slider.getValue());}
        else if(which==PREDHPSMINM){wrap.setLabelSpecial(Para.PREDHPSMINM = slider.getValue());}
        else if(which==PREDHPSLEN){Para.PREDHPSLEN = slider.getValue();wrap.setLabel(slider.getValue());}
    }
}